package grupobbva.pe.operhc.util;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.RSAPrivateKeySpec;

import javax.crypto.Cipher;

import org.apache.log4j.Logger;

public class DecifraRSA {
	
	private static Logger logger = Logger.getLogger(DecifraRSA.class);
		
	public static String decifraMensaje(String hexa) throws Exception {	
		byte[] encriptado = new BigInteger(hexa, 16).toByteArray();
		logger.info(new String(encriptado)); 
		
		BigInteger m = new BigInteger("9592302915668374591574941692139895848000754093203642725640118138745406111818348609338268189634169147832955580073749592851591652411601200275199607319050229");
		BigInteger e = new BigInteger("6946468351887041794957760237865014525323340849648975140132749543995864535558378448477513698914558760388565690107304232148682499218365245544491336831877553");
		
		RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
	    KeyFactory fact = KeyFactory.getInstance("RSA");
	    PrivateKey privateKey = fact.generatePrivate(keySpec);
		
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] cipherData = cipher.doFinal(encriptado);
		logger.info("CipherData (Length): " + cipherData.length);
		/*
		for (Byte b : cipherData) {
			logger.info(b + ",");
		}
		*/
		String mensaje = new String(cipherData);
		logger.info(mensaje);
		return mensaje;
	}
	
	public static void main(String[] args) {
		try {
			DecifraRSA.decifraMensaje("589414B06988743FA9BEA945E3AAE6C36C6C546749E41A29C12DD087A9B02A12E2FDA8D28C1821082B9E1A96AF11D082B61C18A1538CA6ADA8CFF1377FAD7EC0");
			//DecifraRSA.decifraMensaje("1795441D4DA7D75C4315AE0B86D295FD72A9BE54DE21CF09A8207D929D74E0792AB70B0F9B8E4F4BD2DED318BE5D5F4A25E1C53D978E0AC06F89F9E778132F42");
			//DecifraRSA.decifraMensaje("08CC7586271CE4322C915276401CA5347A95F3BCC38E40593928C8A92498F32871CF7589696AE04ACA30C0C778889825B16170E035B9770D46104D78256D774A");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
